import { ref, toValue } from 'vue'
export default function useTable(getTableFn, searchForm) {
  const tableData = ref([])
  const listLoading = ref(false)
  const pagination = ref({ total: 0, pageSize: 10, pageNumber: 1 })
  // 刷新表格
  const refreshTable = function () {
    getTable()
  }
  // 拉表格数据
  const getTable = async function () {
    var { pageSize, pageNumber } = pagination.value
    try {
      listLoading.value = true
      const postData = toValue(searchForm)
        ? { ...toValue(searchForm), pageSize, pageNumber }
        : { pageSize, pageNumber }
      const response = await getTableFn(postData)
      listLoading.value = false
      tableData.value = response.data
      pagination.value = response.pagination
    } catch (error) {
      console.log('error: ', error)
      listLoading.value = false
    }
  }
  getTable()
  return { tableData, listLoading, pagination, refreshTable }
}
